package com.base.cn.platform.os.timing;

import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.j2cache.J2CacheRegion;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.entity.manage.count.PageViewCount;
import com.base.cn.platform.os.service.manage.count.PageUVCountBiz;
import com.base.cn.platform.os.service.manage.count.PageViewCountBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 统计pv uv定时任务
 * zjh 2019.3.25
 */

@Component
@Lazy(false)
public class PvAndUvScheduledTask {

    //缓存KEY
    private static final String DATA_CACHE_KEY = "page_count_data_cache_key";
    @Autowired
    private PageViewCountBiz pageViewCountBiz;
    @Autowired
    private PageUVCountBiz pageUVCountBiz;
    @Autowired
    private CustomCacheChannel customCacheChannel;

    /**
     *每天执行一次
     */
    @Scheduled(cron = "0 0 0 1/1 * ?")
    public void logTime(){
            savePvAndUv();
    }
    private void savePvAndUv(){
        List<PageViewCount> pageViewCountList = (List<PageViewCount>)customCacheChannel.get(J2CacheRegion.ONE_DAY_REGION.getKey(),DATA_CACHE_KEY).getValue();
        if(ObjectUtils.isNotEmpty(pageViewCountList)){
            //添加PV
            pageViewCountBiz.addPageViewCount(pageViewCountList);
            //添加UV
            pageUVCountBiz.addPageUVCount(pageViewCountList);
            //移除缓存
            customCacheChannel.evict(J2CacheRegion.ONE_DAY_REGION.getKey(),DATA_CACHE_KEY);
        }
    }

}
